import request from "@/utils/request";
import {loginInfo,tokenInfo} from '../types/data'
import { RootThunkAction } from "../types/store";
import {ApiResponse} from '../types/data'
import { setToken } from "@/utils/storage";
// 用户登录的dispatch
export const userLogin=(values:loginInfo):RootThunkAction=>{
    return async (dispatch)=>{
        // 1.发送登录的请求  
        // 本接口的返回值类型tokenInfo
        const res = await request.post<ApiResponse<tokenInfo>>('/authorizations',values)
        // 不能这么写
        // const res = await request<ApiResponse<tokenInfo>>({
        //     url:""
        // })
        console.log(res);
        // 2.持久化
        setToken(res.data.data)
        // 3.存到redux
        dispatch({
            type:"login/getInfo",
            payload:res.data.data
        })
        
    }
}
// 获取验证码的action
export const getCode=(mobile:string):RootThunkAction=>{
    return async (dispatch)=>{
        // 1.发送验证码的请求
        await request.get(`/sms/codes/${mobile}`)
    }
}

